const db = require("../db/index"); // 数据库处理模块
// 获取所有商品列表
exports.getGoodsList = (req, res) => {
  const sql = `SELECT * FROM goodslist`;
  db.query(sql, (err, data) => {
    if (err) return res.send(err);
    data.map((item) => {
      item.code = item.code == 0 ? "上架" : "下架";
      switch (item.classify) {
        case 0:
          item.classify = "卧室系列";
          break;
        case 1:
          item.classify = "客厅系列";
          break;
        case 2:
          item.classify = "餐厅系列";
          break;
      }
    });
    res.send(data);
  });
};
// 添加商品列表
exports.addGoods = (req, res) => {
  let sql = `INSERT INTO goodslist SET ?`;
  let uploadData = req.body.uploadObj;
  uploadData.sales = 0;
  db.query(sql, uploadData, (err, data) => {
    if (err) return res.send(err);
    res.send({ code: 200, message: "添加商品成功", data });
  });
};
// 更改商品状态
exports.alterGoods = (req, res) => {
  const sql = `update goodslist set code = ${req.body.code} where id = ${req.body.id}`;
  db.query(sql, (err, data) => {
    if (err) return res.send(err);
    res.send({ code: 200, message: "操作成功" });
  });
};
